package com.hotel.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotel.bean.RespBean;
import com.hotel.bean.User;
import com.hotel.exception.SteduException;
import com.hotel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/user")
public class UserController {
    @Autowired
    private UserService userService;
    //办理入住
    @PostMapping
    public RespBean insert(@RequestBody @Validated User user) throws SteduException {
        if(userService.insert(user)){
            return RespBean.ok("入住成功！！！");
        }
        return RespBean.error("添加失败请联系管理员");
    }
    //修改
    @PutMapping
    public RespBean update(@RequestBody @Validated User user) throws SteduException{
        if(userService.update(user)){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败请联系管理员");
    }
    //根据uid删除
    @DeleteMapping("/{uid}")
    public RespBean deleteByUid(@PathVariable Integer uid) throws SteduException{
        if(userService.updateUstate(uid)){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败请联系管理员");
    }
    //根据uid查询
    @GetMapping("/{uid}")
    public RespBean selectByUid(@PathVariable Integer uid){
        if(userService.selectByUid(uid) != null){
            return RespBean.ok("",userService.selectByUid(uid));
        }
        return RespBean.error("无法查询到该用户，请联系管理员");
    }
    //查询所有(根据姓名查询)
    @GetMapping
    public RespBean selectAll(Integer pageNum,String uname){
        if(pageNum == null){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,5);
        List<User> list = userService.selectAll(uname);
        PageInfo<User> info = new PageInfo<>(list);
        return RespBean.ok("查询成功",info);
    }
    //@GetMapping("/export")
    //public void export(HttpServletResponse response) throws SteduException, IOException {
    //    List<User> all = userService.selectAll(null);
    //    if(CollectionUtil.isEmpty(all)){
    //        throw new SteduException("未找到数据");
    //    }
    //    List<Map<String, Object>> list = new ArrayList<>(all.size());
    //    for(User user : all){
    //        Map<String, Object> row = new HashMap<>();
    //        row.put("ID",user.getUid());
    //        row.put("姓名",user.getUname());
    //        row.put("身份证号码",user.getUnumber());
    //        row.put("电话",user.getUphone());
    //        row.put("入住房号",user.getRnumber());
    //        row.put("入住时间",user.getUstart());
    //        row.put("退房时间",user.getUend());
    //        row.put("状态",user.getUstate());
    //        list.add(row);
    //    }
    //    ExcelWriter writer = ExcelUtil.getWriter(true);
    //    writer.write(list,true);
    //    response.setContentType("text/html");
    //    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    //    response.setHeader("Content-disposition","attachment;filename=user.xlsx");
    //    ServletOutputStream out = response.getOutputStream();
    //    writer.flush(out,true);
    //    writer.close();
    //    IoUtil.close(System.out);
    //   //return RespBean.ok("");
    //}
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        userService.exportExcel(response);
    }
}
